package com.xs.ahoi.hcxs.dao;

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 * author: ahoi
 * date: 2018/6/21 13:28
 * description: 收藏路径
 */
public class CollectPathDao {

    private static CollectPathDao instance;
    private static SQLiteDatabase db;

    private CollectPathDao() {
    }

    public static synchronized CollectPathDao getInstance() {
        if (db == null) {
            db = DBHelper.getInstance().getDb();
        }
        if (instance == null) {
            instance = new CollectPathDao();
        }
        return instance;
    }

    // 添加路径
    public void save(String path) {
        String sql = "insert into " + DBHelper.TABLE_NAME_COLLECT_PATH + " (path) values (?);";
        db.execSQL(sql, new Object[]{path});
    }

    // 删除路径
    public void deleteById(int id) {
        String sql = "delete from " + DBHelper.TABLE_NAME_COLLECT_PATH + " where id = " + id;
        db.execSQL(sql);
    }

    // 删除路径
    public void deleteByPath(String path) {
        String sql = "delete from " + DBHelper.TABLE_NAME_COLLECT_PATH + " where path = '" + path + "'";
        db.execSQL(sql);
    }

    // 查询所有路径
    public List<String> listAll() {
        Cursor cursor = null;
        List<String> list = new ArrayList<>();
        String sql = "select path from " + DBHelper.TABLE_NAME_COLLECT_PATH;
        try {
            cursor = db.rawQuery(sql, new String[]{});
            while (cursor.moveToNext()) {
                list.add(cursor.getString(0));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

        return list;
    }
//
//    private CollectPath cursorToModel(Cursor cursor) {
//        CollectPath model = new CollectPath();
//        model.setPath(cursor.getString(1));
//        return model;
//    }

}
